Imports System.Windows.Forms
Imports System.IO
Public Class dlgDish

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim sql As String
        If db.getvalue("select * from Dish_CuChi  where DishName = '" & cbDishName.Text.Replace("'", "''") & "'").Tables(0).Rows.Count > 0 Then
            MsgBox("Duplicate!!!")
            Exit Sub
        End If

        sql = "insert into Dish_CuChi (Dishname,DishDesc)" _
        & " values('" & cbDishName.Text.Replace("'", "''") _
        & "','" & txtDishDesc.Text.Replace("'", "''") & "')"
        Try
            db.update(sql)
            saveImage()
            MsgBox("The Dish Name '" & cbDishName.Text & "' has been created.")
           bindDish

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub saveImage()
        If CheckBox1.Checked Then
            If Not PictureBox1.Image Is Nothing Then
                Dim mstr As New MemoryStream()
                PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
                Dim arrImage As Byte() = mstr.GetBuffer()
                db.InsertImageToDish(mstr.GetBuffer(), db.getvalue("select * from Dish_CuChi  where DishName = '" & cbDishName.Text.Replace("'", "''") & "'", "DishID"))
            Else
                db.update("update Dish_CuChi  set dishimage = NULL where dishName= '" & cbDishName.Text.Replace("'", "''") & "'")
            End If
        Else
            db.update("update Dish_CuChi  set dishimage = NULL where dishName= '" & cbDishName.Text.Replace("'", "''") & "'")
        End If
        
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim sql As String
        'If db.getvalue("select * from Dish where Dishname = '" & cbDishName.Text.Replace("'", "''") & "'").Tables(0).Rows.Count <= 0 Then
        '    MsgBox("The Dish Name " & cbDishName.Text & " is not found!!!")
        '    Exit Sub
        'End If
        If db.getvalue("select * from Dish_CuChi  where DishName = '" & cbDishName.Text.Replace("'", "''") & "' and DishID <>" & txtDishID.Text).Tables(0).Rows.Count > 0 Then
            MsgBox("Duplicate!!!")
            Exit Sub
        End If
        sql = "update Dish_CuChi  set " _
        & " DishName = '" & cbDishName.Text.Replace("'", "''") _
        & "', DishDesc = '" & txtDishDesc.Text.Replace("'", "''") _
        & "' where DishID =" & txtDishID.Text & ""
        Try
            db.update(sql)
            saveImage()
            MsgBox("The Dish ID " & txtDishID.Text & " has been updated.")
            bindDish()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub cbDishName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDishName.SelectedIndexChanged
        Dim ds As DataSet
        'On Error Resume Next
        ds = db.getvalue("select * from Dish_CuChi  where DishName='" & cbDishName.Text.Replace("'", "''") & "'")
        If ds.Tables(0).Rows.Count > 0 Then
            txtDishDesc.Text = ds.Tables(0).Rows(0).Item("DishDesc").ToString
            txtDishID.Text = ds.Tables(0).Rows(0).Item("DishID").ToString
            Try
                Dim b As Byte()
                b = CType(ds.Tables(0).Rows(0).Item("dishimage"), Byte())
                Dim mem As New MemoryStream(b)
                PictureBox1.Image = Image.FromStream(mem)
                CheckBox1.Checked = True
            Catch ex As Exception
                PictureBox1.Image = Nothing
                CheckBox1.Checked = False
            End Try
            
        End If
    End Sub

    Private Sub dlgDish_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bindDish
    End Sub
    Private Sub bindDish()
        cbDishName.DataSource = db.getvalue("select * from Dish_CuChi ").Tables(0)
        cbDishName.DisplayMember = "DishName"
    End Sub

    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim OpenFileDialog1 As New OpenFileDialog
            With OpenFileDialog1
                '.InitialDirectory = "C:\"
                .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
                .FilterIndex = 4
            End With

            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                PictureBox1.ImageLocation = OpenFileDialog1.FileName
                CheckBox1.Checked = True
            End If
        End If
    End Sub


End Class
